
package hasthi.metamodel;

import hasthi.common.ChangeListener;
import hasthi.metamodel.ChangeEvent.ChangeType;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.namespace.QName;

/**
 * Keep track of the changes happend. This is used in resource model as well as 
 * heart beaat capability.
 * @author Srinath Perera(hperera@cs.indiana.edu)
 */

public class ChangeBuffer implements ChangeListener{
    private List<ChangeEvent> changes = new Vector<ChangeEvent>();
    
    
    public void resourceAdded(QName resourceName) {
        changes.add(new ChangeEvent(ChangeType.AddNew, resourceName,System.currentTimeMillis()));
    }

    public void resourceRemoved(QName resourceName) {
        changes.add(new ChangeEvent(ChangeType.Remove, resourceName,System.currentTimeMillis()));
    }

    public void propertyAdded(QName resourceName, QName propertyName) {
    }

    public void propertyChanged(QName resourceName, QName propertyName, Object newValue) {
        changes.add(new ChangeEvent(ChangeType.Update, resourceName,System.currentTimeMillis(),propertyName,newValue));    
    }

    public void propertyRemoved(QName resourceName, QName propertyName) {
    }
    
    
    public List<ChangeEvent> reset(){
        synchronized (changes) {
            List<ChangeEvent> returnVal = new ArrayList<ChangeEvent>(changes);
            changes.clear();
            //System.out.print(returnVal.size() + " ");
            return returnVal;
        }
    }
}

